package com.laikan.reader.core.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.reader.R;
import com.laikan.reader.core.LaikanBaseAdapter;
import com.laikan.reader.core.bean.FocusCommentBean;
import com.laikan.reader.core.utils.PublicUtil;
import com.laikan.reader.core.view.MyTextView;

import java.util.ArrayList;

/**
 * Created by admin on 2016/11/7.
 * 看点评论列表适配器
 */
public class FocusCommentAdapter extends LaikanBaseAdapter<FocusCommentBean.DataBean> {

    public Drawable C_999999_reply, C_999999_clicktosupport, C_66ccdd_clicktosupport;
    private ItemClickListener itemClickListener;

    public FocusCommentAdapter(Context context, ArrayList<FocusCommentBean.DataBean> datas) {
        super(context, datas);
        C_66ccdd_clicktosupport = new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext, R.drawable.iv_click_to_support), Color.parseColor("#66ccdd")));
        C_999999_clicktosupport = new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext, R.drawable.iv_click_to_support), Color.parseColor("#999999")));
        C_999999_reply = new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext, R.drawable.iv_reply), Color.parseColor("#999999")));
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.lk_comment_item_newcopy, null);
            holder.username = (TextView)convertView.findViewById(R.id.head_reviewer_name);
            holder.person_img = (ImageView)convertView.findViewById(R.id.head_reviewer_img);
            holder.comment_ding = (ImageView)convertView.findViewById(R.id.comment_ding);
            holder.comment_content = (TextView)convertView.findViewById(R.id.head_comment_content);
            holder.comment_title = (MyTextView)convertView.findViewById(R.id.comment_title);
            holder.comment_title.setVisibility(View.GONE);
            holder.comment_time = (TextView)convertView.findViewById(R.id.head_comment_time);
            holder.iv_click_to_support = (ImageView)convertView.findViewById(R.id.iv_clicktosupport);
            holder.iv_reply = (ImageView)convertView.findViewById(R.id.iv_reply);
            holder.tv_click_to_support = (TextView)convertView.findViewById(R.id.tv_clicktosupport);
            holder.tv_reply = (TextView)convertView.findViewById(R.id.tv_reply);
            holder.lr_item_root = (RelativeLayout)convertView.findViewById(R.id.lr_item_content);
            holder.rl_clicktosupport = (RelativeLayout)convertView.findViewById(R.id.rl_clicktosupport); //点赞
            holder.rl_clickreply = (RelativeLayout)convertView.findViewById(R.id.rl_clickreply);  //回复
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        if (datas.get(position).isPraise()) {
            holder.iv_click_to_support.setImageDrawable(C_66ccdd_clicktosupport);
        }else {
            holder.iv_click_to_support.setImageDrawable(C_999999_clicktosupport);
        }
        //回复数
        holder.iv_reply.setImageDrawable(C_999999_reply);
        if(!TextUtils.isEmpty(datas.get(position).getReviewReplyNum()) && datas.get(position).getReviewReplyNum().length() > 2){
            holder.tv_reply.setText(R.string.tv_comment_more);
        }else {
            holder.tv_reply.setText(datas.get(position).getReviewReplyNum());
        }
        //点赞数
        if(!TextUtils.isEmpty(datas.get(position).getReviewPraiseNum()) && datas.get(position).getReviewPraiseNum().length() > 2){
            holder.tv_click_to_support.setText(R.string.tv_comment_more);
        }else {
            holder.tv_click_to_support.setText(datas.get(position).getReviewPraiseNum());
        }
        final ViewHolder finalHolder = holder;
        holder.rl_clicktosupport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.onClickToSupport(finalHolder.iv_click_to_support, position);
            }
        });

        holder.lr_item_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.onReplyClick(view, position);
            }
        });
        holder.rl_clickreply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.onReplyClick(view, position);
            }
        });
        holder.username.setText(datas.get(position).getReviewName());
        holder.comment_time.setText(datas.get(position).getCreateTime());
        holder.person_img.setImageURI(Uri.parse(datas.get(position).getReviewPic()));
        holder.comment_content.setText(datas.get(position).getContent());

        return convertView;
    }

    private class ViewHolder {

        private ImageView person_img, comment_ding, iv_click_to_support, iv_reply;;
        private TextView comment_time, comment_content, username, comment_jing, tv_click_to_support, tv_reply;
        private MyTextView comment_title;
        private RelativeLayout lr_item_root, rl_clicktosupport, rl_clickreply;
    }

    public void setItemClickListener(ItemClickListener listener){
        this.itemClickListener = listener;
    }
    public interface ItemClickListener{
        void onItemClick(View view, int position);
        void onReplyClick(View view, int position);
        void onClickToSupport(View view, int position);
    }
}

